package de.jare.photoswapper;

import java.io.IOException;
import java.lang.*;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class photoswapper extends Activity implements SurfaceHolder.Callback {
    /** Called when the activity is first created. */
	
	private NotificationManager mManager;
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    HttpHandler h = new HttpHandler();

	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        setContentView(R.layout.main);
        mSurfaceView = (SurfaceView)findViewById(R.id.surface);

        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        h.AndroidID = System.getString(this.getContentResolver(), System.ANDROID_ID);
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }
 
        if (keyCode == KeyEvent.KEYCODE_SPACE) {
        	Log.d("PS", "photoswapper.onKeyDown(): takePicture()...");
            mCamera.takePicture(null, null, jpegCallback);
            return true;
        }

        return false;
    }
        
     PictureCallback jpegCallback = new PictureCallback() {
           public void onPictureTaken(byte[] data, Camera c) { 
        	   Log.d("PS", "photoswapper.jpegCallback: PictureCallback()...");
        	   h.UploadPicture(data);
        	   h.ReceivePicture();
        	   mCamera.startPreview();
           }
     };
     
     private void notifyMessage(String msg) {
    	 String svcName = Context.NOTIFICATION_SERVICE;
         mManager = (NotificationManager)getSystemService(svcName);
         // Choose a drawable to display as the status bar icon
         int icon = R.drawable.icon;
         // Text to display in the status bar when the notification is launched
         String tickerText = "Notification";
         // The extended status bar orders notification in time order
         long when = java.lang.System.currentTimeMillis();
         Notification notification = new Notification(icon, tickerText, when);
         Context context = getApplicationContext();
         // Text to display in the extended status window
         String expandedText = msg;
         // Title for the expanded status
         String expandedTitle = "Notification Title";
         // Intent to launch an activity when the extended text is clicked
         Intent intent = new Intent(this, photoswapper.class);
         PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
         notification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);
         mManager.notify(0, notification);
     }
   

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
    	
        mCamera = Camera.open();
        Camera.Parameters parameters = mCamera.getParameters();
    	parameters.setPictureFormat(PixelFormat.JPEG);
    	parameters.setPictureSize(640, 480);
    	mCamera.setParameters(parameters);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        mCamera.startPreview();
    }

}